﻿{%- if IsNewableObject -%}
{{ Variable }} = {{ Value }} ? {{ Value }}.toJSON() : <any>{{ NullValue }};
{%- elsif IsArray -%}
if (Array.isArray({{ Value }})) {
    {{ Variable }} = [];
    for (let item of {{ Value }})
{%-     if IsArrayItemNewableObject -%}
        {{ Variable }}.push(item.toJSON());
{%-     elsif IsArrayItemDate -%}
        {{ Variable }}.push({% if UseJsDate %}formatDate(item){% else %}item.{{ DateToStringCode }}{% endif %});
{%-     elsif IsArrayItemDateTime -%}
        {{ Variable }}.push(item.{{ DateTimeToStringCode }});
{%-     else -%}
        {{ Variable }}.push(item);
{%-     endif -%}
}
{%- elsif IsDictionary -%}
if ({{ Value }}) {
    {{ Variable }} = {};
    for (let key in {{ Value }}) {
        if ({{ Value }}.hasOwnProperty(key))
{%-     if IsDictionaryValueNewableObject -%}
            (<any>{{ Variable }})[key] = {{ Value }}[key] ? {{ Value }}[key].toJSON() : <any>{{ NullValue }};
{%-     elsif IsDictionaryValueDate -%}
            (<any>{{ Variable }})[key] = {{ Value }}[key] ? {% if UseJsDate %}formatDate({{ Value }}[key]){% else %}{{ Value }}[key].{{ DateToStringCode }}{% endif %} : <any>{{ NullValue }};
{%-     elsif IsDictionaryValueDateTime -%}
            (<any>{{ Variable }})[key] = {{ Value }}[key] ? {{ Value }}[key].{{ DateTimeToStringCode }} : <any>{{ NullValue }};
{%-     else -%}
{%-         if NullValue != "undefined" -%}
            (<any>{{ Variable }})[key] = {{ Value }}[key] !== undefined ? {{ Value }}[key] : <any>{{ NullValue }};
{%-         else -%}
            (<any>{{ Variable }})[key] = {{ Value }}[key];
{%-         endif -%}
{%-     endif -%}
    }
}
{%- elsif IsDate -%}
{{ Variable }} = {{ Value }} ? {% if UseJsDate %}formatDate({{ Value }}){% else %}{{ Value }}.{{ DateToStringCode }}{% endif %} : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%- elsif IsDateTime -%}
{{ Variable }} = {{ Value }} ? {{ Value }}.{{ DateTimeToStringCode }} : {% if HasDefaultValue %}{{ DefaultValue }}{% else %}<any>{{ NullValue }}{% endif %};
{%- elsif NullValue != "undefined" -%}
{{ Variable }} = {{ Value }} !== undefined ? {{ Value }} : <any>{{ NullValue }};
{%- else -%}
{{ Variable }} = {{ Value }};
{%- endif %}